package entity.terrain;

import image.ImageCache;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import entity.Sprite;

/**
 * The objects that the Lemmings interact with. Terrain can be either 
 * destructable or non-destructable, fatal or innocuous. 
 * 
 * @author Neil Cowzer, Olga Murdoch
 */
public class Terrain extends Sprite{
	/** The fatal/innocuous of this Terrian. */
	private boolean fatal;
	
	/** The destructabiltity value of this Terrain. */
	private boolean destructable;
	/*@ invariant (fatal == true && destructable = false) || 
				  (destructable == true || fatal == false) ||
				  (destructable == false || fatal == false);
	@*/
	
	/**
	 * Create a Terrain instance.
	 * 
	 * @param ref the relative (String) address of the Image.
	 * @param x the x-coordinate of the Terrain
	 * @param y the y coordinate of the Terrain
	 * @param fatal
	 * @param destructable
	 */
	/*@ requires ref != null && 0 <= x && 0 <= y;
	  	assignable fatal && destructable;
	  	ensures true;
	@*/
	public Terrain(String ref,int frames, boolean fatal, boolean destructable){
		super(ref, frames, 0, 0);
		this.fatal = fatal;
		this.destructable = destructable;
	}
	
	/**
	 * Check whether this Terrain is destructable.
	 * 
	 * @return true if Terrain is destructable.
	 */
	public /*@ pure @*/ boolean isDestructable() {
		return destructable;
	}
	
	/**
	 * Check whether a Terrain is innocuous or fatal.
	 * 
	 * @return true if a Terrain is fatal.
	 */
	public /*@ pure @*/ boolean isFatal() {
		return fatal;
	}
	
	/**
	 * Create new Terrain at give coordinates.
	 * 
	 * @param x the x-coordinate of the Terrain
	 * @param y the y-coordinate of the Terrain
	 */
	//@ requires (* x && y withinbounds *);
	public void createTerrain(int x, int y){
		
	}
	
	/**
	 * Destroy the Terrain at the given coordinates.
	 * 
	 * @param x x the x-coordinate of the Terrain
	 * @param y the y-coordinate of the Terrain
	 * @param type of destruction of terrain, circular or block 
	 */
	/*@ requires (* x && y withinbounds *) && (type == "block" || type == "circular");
	 * 	assignable terrain;
	@*/
	public void destroyTerrain(int x, int y, String type){
		BufferedImage land = getImage().getBufferedImage();
		int black = 16777215; // our black
		
		/* Still to add mining, bashing .... */
		
		if (type.equals("digger")){
			if (new Rectangle(x,y, x+17, y+1).intersects(getBounds())){
				for (int i =x; i < x+17; i++){
					land.setRGB(i, y+1, black);
				}
			}	
		}
		else if (type.equals("bomber")){
			if (new Rectangle(x,y, x+15, y+5).intersects(getBounds())){
					for (int i = y; i < y+5; i++){
						for (int j = x; j < x+15; j++){
							if (j >= x+(i-y) && j <= (x+15)-(i-y)){
								land.setRGB(j, i, black);
							}
						}
					}
			}
		}
		else{
			// ignore & possibly return an error
			System.out.println("ivalid destruction type.");
		}
		
		// overwrite orginal image!
		ImageCache imc = new ImageCache();
		imc.update(getRef(), land);
		/*img = null;
		img = imc.getSprite(getRef());*/
	}

	
}
